package com.ruoyi.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.TProductCategory;
import com.ruoyi.common.core.redis.RedisCache;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.CategoryTree;
import com.ruoyi.business.service.ICategoryTreeService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 美团树形结构Controller
 * 
 * @author 超
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/business/tree")
public class CategoryTreeController extends BaseController
{
    @Autowired
    private ICategoryTreeService categoryTreeService;

    @Autowired
    private RedisCache redisCache;
    /**
     * 查询美团树形结构列表  无理由退货标签名称
     */
    @PreAuthorize("@ss.hasPermi('business:tree:list')")
    @GetMapping("/list")
    public AjaxResult list(CategoryTree categoryTree) {
        String cacheKey = "categoryTreeList:" + categoryTree.toString();
        List<CategoryTree> list = (List<CategoryTree>) redisCache.getCacheObject(cacheKey);
        if (list == null) {
            list = categoryTreeService.selectCategoryTreeList(categoryTree);
            redisCache.setCacheObject(cacheKey, list);
        }
        return success(list);
    }

    @ApiOperation("树形结构查询")
    @PreAuthorize("@ss.hasPermi('business:category:tree')")
    @GetMapping("/CategoryTree")
    public AjaxResult CategoryTree(CategoryTree tProductCategory)
    {
        List<CategoryTree> list = categoryTreeService.CategoryTree(tProductCategory);
        return success(list);
    }


    // 新增操作后清除缓存
    @PreAuthorize("@ss.hasPermi('business:tree:add')")
    @Log(title = "美团树形结构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CategoryTree categoryTree) {
        AjaxResult result = toAjax(categoryTreeService.insertCategoryTree(categoryTree));
        clearCache();
        return result;
    }

    // 修改操作后清除缓存
    @PreAuthorize("@ss.hasPermi('business:tree:edit')")
    @Log(title = "美团树形结构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CategoryTree categoryTree) {
        AjaxResult result = toAjax(categoryTreeService.updateCategoryTree(categoryTree));
        clearCache();
        return result;
    }

    // 删除操作后清除缓存
    @PreAuthorize("@ss.hasPermi('business:tree:remove')")
    @Log(title = "美团树形结构", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        AjaxResult result = toAjax(categoryTreeService.deleteCategoryTreeByIds(ids));
        clearCache();
        return result;
    }

        // 清除缓存的方法
        private void clearCache() {
        redisCache.deleteObject("categoryTreeList:*");
    }
    /**
     * 导出美团树形结构列表
     */
    @PreAuthorize("@ss.hasPermi('business:tree:export')")
    @Log(title = "美团树形结构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CategoryTree categoryTree)
    {
        List<CategoryTree> list = categoryTreeService.selectCategoryTreeList(categoryTree);
        ExcelUtil<CategoryTree> util = new ExcelUtil<CategoryTree>(CategoryTree.class);
        util.exportExcel(response, list, "美团树形结构数据");
    }

    /**
     * 获取美团树形结构详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:tree:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(categoryTreeService.selectCategoryTreeById(id));
    }

//    /**
//     * 新增美团树形结构
//     */
//    @PreAuthorize("@ss.hasPermi('business:tree:add')")
//    @Log(title = "美团树形结构", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody CategoryTree categoryTree)
//    {
//        return toAjax(categoryTreeService.insertCategoryTree(categoryTree));
//    }
//
//    /**
//     * 修改美团树形结构
//     */
//    @PreAuthorize("@ss.hasPermi('business:tree:edit')")
//    @Log(title = "美团树形结构", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody CategoryTree categoryTree)
//    {
//        return toAjax(categoryTreeService.updateCategoryTree(categoryTree));
//    }
//
//    /**
//     * 删除美团树形结构
//     */
//    @PreAuthorize("@ss.hasPermi('business:tree:remove')")
//    @Log(title = "美团树形结构", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        return toAjax(categoryTreeService.deleteCategoryTreeByIds(ids));
//    }
}
